import java.util.Arrays;
import java.util.LinkedList;



// https://leetcode-cn.com/problems/queue-reconstruction-by-height/
// 406. 根据身高重建队列

class Solution {
    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, (a, b) -> {
            if (a[0] == b[0]) {
                return a[1] - b[1];
            } else {
                return b[0] - a[0];
            }

        });

        LinkedList<int[]> que = new LinkedList<>();

        for (int[] is : people) {
            que.add(is[1], is);
        }

        return que.toArray(new int[people.length][]);
    }
}